package com.ybkj.daijia.server.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.config.RentApi;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mc.Enterprise;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.mc.PassengerAccount;
import com.ybkj.daijia.server.model.Peccancy;
import com.ybkj.daijia.server.model.RentOrder;
import com.ybkj.daijia.server.model.RentOrderStatus;
import com.ybkj.daijia.server.service.MemberService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping(value = "rentorder")
public class RentOrderController {

    @Autowired
    private RentApi rentApi;
    @Autowired
    private SettingUtils settingUtils;
    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "neworder", method = RequestMethod.GET)
    public ModelAndView neworder() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("rentorder/neworder");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "backpay", method = RequestMethod.GET)
    public ModelAndView backpay(@RequestParam("id") Long id) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("id", id);
            modelAndView.setViewName("rentorder/backpay");
            return modelAndView;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "orderinfo", method = RequestMethod.GET)
    public ModelAndView orderinfo(@RequestParam("id") Long id) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("id", id);
            modelAndView.setViewName("rentorder/orderinfo");
            return modelAndView;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "cancellist", method = RequestMethod.GET)
    public ModelAndView contentlist() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("rentorder/cancel");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "overlist", method = RequestMethod.GET)
    public ModelAndView overlist() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("rentorder/over");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "backlist", method = RequestMethod.GET)
    public ModelAndView backlist() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("rentorder/back");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "depositlist", method = RequestMethod.GET)
    public ModelAndView depositlist() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("rentorder/deposit");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "prepay", method = RequestMethod.GET)
    public ModelAndView prepay() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("rentorder/prepay");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "peccancy", method = RequestMethod.GET)
    public ModelAndView peccancy() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("rentorder/peccancy");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "serverlist", method = RequestMethod.GET)
    public ModelAndView serverlist() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("rentorder/server");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "confirmlist", method = RequestMethod.GET)
    public ModelAndView confirmlist() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentorder")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("rentorder/confirm");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "confirm", method = RequestMethod.POST)
    public Map<String, Object> confirm(@RequestParam("id") Long id,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.CONFIRM_RENTORDER, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "确定成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "prePay", method = RequestMethod.POST)
    public Map<String, Object> prePay(@RequestParam("id") Long id,
        @RequestParam("payed") Double payed,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        //获取订单信息
        RentOrder order = selectRentOrder(id, response);
        if (null != order) {
            //获取客户信息
            Passenger passenger = memberService.findOnePassenger(order.getPassengerId());
            if (null != passenger) {//余额支付
                //获取客户余额
                Double cash = passenger.getDrawMoney().add(passenger.getNodrawMoney())
                    .doubleValue();
                if (payed > cash) {//客户余额不足
                    map.put("message", "余额不足");
                    map.put("success", false);
                    map.put("code", -1);
                } else {//客户余额充足，余额扣除
                    Result<PassengerAccount> result = new Result<PassengerAccount>();
                    if (passenger.getType().equals(Passenger.TYPE_PASSENGER)) {
                        //更新个人客户信息和添加账户信息
                        result = memberService
                            .money(passenger, null, new BigDecimal(payed), "结算使用余额支付" + payed + "元",
                                "系统", PassengerAccount.PayType.PAYTYPE_BALANCE,
                                order.getId(), order.getOrderNo(), 3, null, true, true);
                    }
                    if (passenger.getType().equals(Passenger.TYPE_ENTERPRISE)) {
                        Enterprise enterprise = memberService
                            .findOneEnterprise(passenger.getEnterprise_id());
                        result = memberService.moneyEnterprise(enterprise, new BigDecimal(payed),
                            "结算使用余额支付" + payed + "元", "系统",
                            PassengerAccount.PayType.PAYTYPE_BALANCE,
                            order.getId(), order.getOrderNo(), 3, null, true, true);
                    }
                    if (result.isSuccess()) {
//						map.put("message", "支付成功");
//						map.put("success", true);
                        map = this.payed(id, payed, response);
                    } else {
                        map.put("message", result.getMessage());
                        map.put("success", false);
                        map.put("code", -1);
                    }
                }
            }
        }
        return map;
    }

    @RequestMapping(value = "payed", method = RequestMethod.POST)
    public Map<String, Object> payed(@RequestParam("id") Long id,
        @RequestParam("payed") Double payed,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        kv.add("payed", String.valueOf(payed));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.PAYED_RENTORDER, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "支付成功");
            map.put("success", true);
            map.put("code", 0);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
            map.put("code", -1);
        }
        return map;
    }


    @RequestMapping(value = "cancel", method = RequestMethod.POST)
    public Map<String, Object> cancel(@RequestParam("id") Long id,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.CANCEL_RENTORDER, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
//	         map.put("message", "销单成功");
//	         map.put("success", true);
            map = backBalance(id, response);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    public Map<String, Object> backBalance(Long id,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        RentOrder order = this.selectRentOrder(id, response);
        if (null != order) {
            //获取客户信息
            Passenger passenger = memberService.findOnePassenger(order.getPassengerId());
            if (null != passenger) {
                //已支付返还订单金额到可提现金额
                passenger
                    .setDrawMoney(passenger.getDrawMoney().add(new BigDecimal(order.getPayed())));
                Result<PassengerAccount> result = new Result<PassengerAccount>();
                if (passenger.getType().equals(Passenger.TYPE_PASSENGER)) {
                    //更新客户信息和添加账户信息
                    result = memberService.money(passenger, null, new BigDecimal(order.getPayed()),
                        "客户消单返还余额" + order.getPayed() + "元", "系统",
                        PassengerAccount.PayType.RECHARGE_TYPE_CANCEL_RETURN,
                        order.getId(), order.getOrderNo(), 3, null, true, true);
                }
                if (passenger.getType().equals(Passenger.TYPE_ENTERPRISE)) {
                    Enterprise enterprise = memberService
                        .findOneEnterprise(passenger.getEnterprise_id());
                    //更新企业客户信息和添加账户信息
                    result = memberService
                        .moneyEnterprise(enterprise, new BigDecimal(order.getPayed()),
                            "客户消单返还余额" + order.getPayed() + "元", "系统",
                            PassengerAccount.PayType.RECHARGE_TYPE_CANCEL_RETURN,
                            order.getId(), order.getOrderNo(), 3, null, true, true);
                }
                if (result.isSuccess()) {
                    map.put("message", "销单成功");
                    map.put("success", true);
                } else {
                    map.put("message", result.getMessage());
                    map.put("success", false);
                }
            } else {
                map.put("message", "没有查到客户信息");
                map.put("success", false);
            }
        }
        return map;
    }

    @RequestMapping(value = "dispatch", method = RequestMethod.POST)
    public Map<String, Object> dispatch(@RequestParam("id") Long id,
        @RequestParam(required = false, value = "driverId") Long driverId,
        @RequestParam(required = false, value = "driverName") String driverName,
        @RequestParam(required = false, value = "driverPhone") String driverPhone,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        if (driverId != null) {
            kv.add("driverId", String.valueOf(driverId));
        }
        if (StringUtils.isNotBlank(driverName)) {
            kv.add("driverName", driverName);
        }

        if (StringUtils.isNotBlank(driverName)) {
            kv.add("driverPhone", driverPhone);
        }

        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.DISPATCH_RENTORDER, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "派单成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "deposit", method = RequestMethod.POST)
    public Map<String, Object> deposit(
        @RequestParam("id") Long id,
        @RequestParam(required = false, value = "prepayRentTime") Long prepayRentTime,
        @RequestParam(required = false, value = "prepayRent") Double prepayRent,
        @RequestParam(required = false, value = "peccancyForegift") Double peccancyForegift,
        @RequestParam(required = false, value = "peccancyForegiftTime") Long peccancyForegiftTime,
        @RequestParam(required = false, value = "prepayRentType") Integer prepayRentType,
        @RequestParam(required = false, value = "peccancyForegiftType") Integer peccancyForegiftType,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();

        kv.add("id", String.valueOf(id));
        if (prepayRentTime != null) {
            kv.add("prepayRentTime", String.valueOf(prepayRentTime));
        }
        if (prepayRent != null) {
            kv.add("prepayRent", String.valueOf(prepayRent));
        }
        if (peccancyForegift != null) {
            kv.add("peccancyForegift", String.valueOf(peccancyForegift));
        }
        if (peccancyForegiftTime != null) {
            kv.add("peccancyForegiftTime", String.valueOf(peccancyForegiftTime));
        }
        if (prepayRentType != null) {
            kv.add("prepayRentType", String.valueOf(prepayRentType));
        }
        if (peccancyForegiftType != null) {
            kv.add("peccancyForegiftType", String.valueOf(peccancyForegiftType));
        }
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.DEPOSIT_RENTORDER, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "押金记录录入成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "server", method = RequestMethod.POST)
    public Map<String, Object> server(@RequestParam("id") Long id,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.SERVER_RENTORDER, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "订单状态修改成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "applyReRent", method = RequestMethod.POST)
    public Map<String, Object> applyReRent(@RequestParam("id") Long id,
        @RequestParam("reRentTime") Long reRentTime,
        @RequestParam("reRentFee") Double reRentFee,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        kv.add("reRentTime", String.valueOf(reRentTime));
        kv.add("reRentFee", String.valueOf(reRentFee));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.APPLYRERENT_RENTORDER, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "订单状态修改成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "back", method = RequestMethod.POST)
    public Map<String, Object> back(
        @RequestParam("id") Long id,
        @RequestParam("backCarTime") Long backCarTime,
        @RequestParam(required = false, value = "error") String error,
        @RequestParam(required = false, value = "errorImage") String errorImage,
        @RequestParam(required = false, value = "backCarEmploy") Long backCarEmploy,
        @RequestParam(required = false, value = "violationFee") Double violationFee,
        @RequestParam(required = false, value = "overtimeFee") Double overtimeFee,
        @RequestParam(required = false, value = "otherFee") Double otherFee,
        @RequestParam(required = false, value = "assessFee") Double assessFee,//
        @RequestParam(required = false, value = "peccancyFee") Double peccancyFee,//
        @RequestParam(required = false, value = "payed") Double payed,
        @RequestParam(required = false, value = "totalFee") Double totalFee,
        @RequestParam(required = false, value = "isPay") Integer isPay,
        @RequestParam(required = false, value = "memo") String memo,
        @RequestParam(required = false, value = "passengerPhone") String passengerPhone,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        if (backCarTime != null) {
            kv.add("backCarTime", String.valueOf(backCarTime));
        }
        if (StringUtils.isNotBlank(error)) {
            kv.add("error", error);
        }
        if (StringUtils.isNotBlank(errorImage)) {
            kv.add("errorImage", errorImage);
        }
        if (assessFee != null) {
            kv.add("assessFee", String.valueOf(assessFee));
        }
        if (peccancyFee != null) {
            kv.add("peccancyFee", String.valueOf(peccancyFee));
        }
        if (backCarEmploy != null) {
            kv.add("backCarEmploy", String.valueOf(backCarEmploy));
        }
        if (violationFee != null) {
            kv.add("violationFee", String.valueOf(violationFee));
        }
        if (overtimeFee != null) {
            kv.add("overtimeFee", String.valueOf(overtimeFee));
        }
        if (otherFee != null) {
            kv.add("otherFee", String.valueOf(otherFee));
        }
        if (payed != null) {
            kv.add("payed", String.valueOf(payed));
        }
        if (totalFee != null) {
            kv.add("totalFee", String.valueOf(totalFee));
        }
        if (isPay != null) {
            kv.add("isPay", String.valueOf(isPay));
        }
        if (StringUtils.isNotBlank(memo)) {
            kv.add("memo", memo);
        }
        if (StringUtils.isNotBlank(passengerPhone)) {
            kv.add("passengerPhone", passengerPhone);
        }
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.BACK_RENTORDER, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "订单状态修改成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "rate", method = RequestMethod.POST)
    public Map<String, Object> rate(@RequestParam("id") Long id,
        @RequestParam("score") Double score,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        kv.add("score", String.valueOf(score));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.RATE_RENTORDER, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "订单状态修改成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "over", method = RequestMethod.POST)
    public Map<String, Object> over(@RequestParam("id") Long id,
        HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin", "*");

        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, String> kv1 = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(RentApi.SELECT_PECCANCYS_BYORDERID)
            .append("?appkey={appkey}");
        if (id != null) {
            kv1.put("orderId", String.valueOf(id));
            builder.append("&orderId={orderId}");
        }
        String result1 = rentApi.get(builder.toString(), kv1, settingUtils.get());
        if (null != result1) {
            JSONObject jo = new JSONObject(result1);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONArray data = jo.getJSONArray("data");
                Gson gson = new GsonBuilder().create();
                List<Peccancy> peccancys = gson
                    .fromJson(data.toString(), new TypeToken<List<Peccancy>>() {
                    }.getType());
                for (Peccancy peccancy : peccancys) {
                    if (peccancy.getStatus() == 0) {
                        map.put("message", "违章未全部处理");
                        map.put("success", false);
                        return map;
                    }
                }
            }
        }

        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.OVER_RENTORDER, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "订单状态修改成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "selectRentOrder", method = RequestMethod.GET)
    public RentOrder selectRentOrder(@RequestParam("id") Long id,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(RentApi.SELECT_RENTORDER)
            .append("?appkey={appkey}");
        Map<String, String> kv = new HashMap<String, String>();
        if (id != null) {
            kv.put("id", id + "");
            builder.append("&id={id}");
        }
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());

        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                Gson gson = new GsonBuilder().create();
                RentOrder rentorder = gson.fromJson(data.toString(), RentOrder.class);
                return rentorder;
            }
        }
        return null;
    }

    @RequestMapping(value = "selectRentOrders", method = RequestMethod.GET)
    public Page<RentOrder> selectRentOrders(
        @RequestParam(required = false, value = "plateNoOrPassengerPhone") String plateNoOrPassengerPhone,
        @RequestParam(required = false, value = "status") Integer status,
        @RequestParam(required = false, value = "rows") Integer rows,
        @RequestParam(required = false, value = "page") Integer page,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());
        Map<String, String> kv = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(RentApi.SELECT_RENTORDERS)
            .append("?appkey={appkey}");
        if (StringUtils.isNotBlank(plateNoOrPassengerPhone)) {
            kv.put("plateNoOrPassengerPhone", plateNoOrPassengerPhone);
            builder.append("&plateNoOrPassengerPhone={plateNoOrPassengerPhone}");
        }
        if (status != null) {
            kv.put("status", String.valueOf(status));
            builder.append("&status={status}");
        }
        page = Pager.getPage();
        rows = Pager.getLimit();
        if (rows != null) {
            kv.put("rows", String.valueOf(rows));
            builder.append("&rows={rows}");
        }
        if (page != null) {
            kv.put("page", String.valueOf(page + 1));
            builder.append("&page={page}");
        }
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        List<RentOrder> rentorders = new ArrayList<RentOrder>();
        long total = 0;
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");

            if (code == 0) {
                JSONObject data = jo.getJSONObject("data");
                total = data.getLong("total");
                JSONArray jsonArray = data.getJSONArray("rows");
                Gson gson = new GsonBuilder().create();
                rentorders = gson.fromJson(jsonArray.toString(), new TypeToken<List<RentOrder>>() {
                }.getType());

            }
        }
        return new PageImpl<RentOrder>(rentorders, pageable, total);
    }

    @RequestMapping(value = "selectRentOrderStatus", method = RequestMethod.GET)
    public List<RentOrderStatus> selectRentOrderStatus(
        @RequestParam("orderId") Long orderId,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, String> kv = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(RentApi.SELECT_RENTORDERSTATUS)
            .append("?appkey={appkey}");
        if (orderId != null) {
            kv.put("orderId", String.valueOf(orderId));
            builder.append("&orderId={orderId}");
        }
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        List<RentOrderStatus> rentOrderStatus = new ArrayList<RentOrderStatus>();
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONArray data = jo.getJSONArray("data");
                Gson gson = new GsonBuilder().create();
                rentOrderStatus = gson
                    .fromJson(data.toString(), new TypeToken<List<RentOrderStatus>>() {
                    }.getType());
                return rentOrderStatus;
            }
        }
        return rentOrderStatus;
    }

    @RequestMapping(value = "addPeccancy", method = RequestMethod.POST)
    public Map<String, Object> addPeccancy(
        @RequestParam("time") Long time,
        @RequestParam("orderId") Long orderId,
        @RequestParam("passengerId") Long passengerId,
        @RequestParam("deductMarks") Double deductMarks,
        @RequestParam("amerce") Double amerce,
        @RequestParam("address") String address,
        @RequestParam("reason") String reason,
        @RequestParam("plateNo") String plateNo,
        @RequestParam("passengerName") String passengerName,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();

        kv.add("time", String.valueOf(time));
        kv.add("orderId", String.valueOf(orderId));
        kv.add("passengerId", String.valueOf(passengerId));
        kv.add("deductMarks", String.valueOf(deductMarks));
        kv.add("amerce", String.valueOf(amerce));
        kv.add("address", address);
        kv.add("reason", reason);
        kv.add("plateNo", plateNo);
        kv.add("passengerName", passengerName);
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.ADD_PECCANCY, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "创建成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "selectPeccancysByOrderId", method = RequestMethod.GET)
    public List<Peccancy> selectPeccancysByOrderId(
        @RequestParam("orderId") Long orderId,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, String> kv = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(RentApi.SELECT_PECCANCYS_BYORDERID)
            .append("?appkey={appkey}");
        if (orderId != null) {
            kv.put("orderId", String.valueOf(orderId));
            builder.append("&orderId={orderId}");
        }
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONArray data = jo.getJSONArray("data");
                Gson gson = new GsonBuilder().create();
                List<Peccancy> peccancys = gson
                    .fromJson(data.toString(), new TypeToken<List<Peccancy>>() {
                    }.getType());
                return peccancys;
            }
        }
        return null;
    }

    @RequestMapping(value = "selectPeccancysByPassengerId", method = RequestMethod.GET)
    public List<Peccancy> selectPeccancysByPassengerId(
        @RequestParam("passengerId") Long passengerId,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, String> kv = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(RentApi.SELECT_PECCANCYS_BYPASSENGERID)
            .append("?appkey={appkey}");
        if (passengerId != null) {
            kv.put("passengerId", String.valueOf(passengerId));
            builder.append("&passengerId={passengerId}");
        }
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONArray data = jo.getJSONArray("data");
                Gson gson = new GsonBuilder().create();
                List<Peccancy> peccancys = gson
                    .fromJson(data.toString(), new TypeToken<List<Peccancy>>() {
                    }.getType());
                return peccancys;
            }
        }
        return null;
    }

    @RequestMapping(value = "dispatchCar", method = RequestMethod.POST)
    public Map<String, Object> dispatchCar(@RequestParam("id") Long id,
        @RequestParam("carId") Long carId,
        @RequestParam("plateNo") String plateNo,
        @RequestParam("vehicle") Long vehicle,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        kv.add("carId", String.valueOf(carId));
        kv.add("plateNo", plateNo);
        kv.add("vehicle", String.valueOf(vehicle));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.DISPATCH_CAR, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "派车成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "backForegift", method = RequestMethod.POST)
    public Map<String, Object> backForegift(@RequestParam("id") Long id,
        @RequestParam("type") Integer type,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        kv.add("type", String.valueOf(type));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.BACKFOREGIFT, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "退还成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "memo", method = RequestMethod.POST)
    public Map<String, Object> memo(
        @RequestParam("id") Long id,
        @RequestParam("memo") String memo,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        kv.add("memo", memo);
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.MEMO_RENTSHOP, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "备注成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "postPeccancy", method = RequestMethod.POST)
    public Map<String, Object> postPeccancy(
        @RequestParam("id") Long id,
        @RequestParam("status") Integer status,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        kv.add("status", String.valueOf(status));
        String result = rentApi
            .postMultiValueMap(rentApi.getApiBaseURL() + RentApi.POST_PECCANCY, kv,
                settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "修改状态成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "selectRentOrdersCount", method = RequestMethod.GET)
    public String selectRentOrdersCount(
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, String> kv = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL()).append(RentApi.SELECT_RENTORDERSCOUNT)
            .append("?appkey={appkey}");
        kv.put("status", String.valueOf(0));
        builder.append("&status={status}");
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Long code = jo.getLong("data");
        StringBuilder builder2 = new StringBuilder();
        builder2.append(rentApi.getApiBaseURL()).append(RentApi.SELECT_RENTORDERSCOUNT)
            .append("?appkey={appkey}");
        kv = new HashMap<String, String>();
        kv.put("status", String.valueOf(50));
        builder2.append("&status={status}");
        String result2 = rentApi.get(builder2.toString(), kv, settingUtils.get());
        JSONObject jo2 = new JSONObject(result2);
        Long code2 = jo2.getLong("data");
        StringBuilder builder3 = new StringBuilder();
        builder3.append(rentApi.getApiBaseURL()).append(RentApi.SELECT_RENTORDERSCOUNT)
            .append("?appkey={appkey}");
        kv = new HashMap<String, String>();
        kv.put("status", String.valueOf(55));
        builder3.append("&status={status}");
        String result3 = rentApi.get(builder3.toString(), kv, settingUtils.get());
        JSONObject jo3 = new JSONObject(result3);
        Long code3 = jo3.getLong("data");
        return "" + code + "," + code2 + "," + code3;
    }
}
